package com.javaGather.day3;

import java.util.List;
import java.util.NoSuchElementException;

public class IteratorDemo {

    public List list;

    public IteratorDemo(List list) {
        this.list = list;
    }

    private int count = 0;

    public Object next() {
        if (list == null) {
            throw new IteratorException("list is null");
        }
        if (count >= list.size()) {
            //访问下标越界
            throw new IteratorException("无法继续向下获取元素");
        }
        return list.get(count++);
    }

    public boolean hasNext() {
        return count != list.size();
    }
}
